#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash redundans-v0.13c.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

get_GapCloser()
{
cnt=1
number=10
while [ $cnt -lt $number ]
do
    set +e
    wget -t 1 -c -P $buildpath http://downloads.sourceforge.net/project/soapdenovo2/GapCloser/src/r6/GapCloser-src-v1.12-r6.tgz
    judge=$?
    if [ $judge -ne 0 ];then
        cnt=$[$cnt+1]
    else
        echo "wget success"
        break
    fi
done

if [ $cnt -eq $number ];then
    cp -r dependency/src/GapCloser-src-v1.12-r6.tgz  $buildpath
    echo "wget failed,cp dependency/src to the buildpath"
fi
set -e 
}

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
yum install -y  environment-modules csh time patch libatomic perl-Data-Dumper autoconf automake libtool numactl binutils systemd-devel valgrind flex wget

set -e
yum install numpy zlib-devel git -y

get_GapCloser
cd $buildpath
wget -t 0 -c https://github.com/lpryszcz/redundans/archive/refs/tags/v0.13c.tar.gz -O $buildpath/redundans-0.13c.tar.gz
wget -t 0 -c https://github.com/lpryszcz/pyScaf/archive/refs/tags/0.12a.tar.gz -O $buildpath/pyScaf-0.12a.tar.gz
wget -t 0 -c https://github.com/lpryszcz/FastaIndex/archive/refs/tags/0.11c.tar.gz -O $buildpath/FastaIndex-0.11c.tar.gz
wget -t 0 -c https://github.com/lh3/bwa/archive/refs/tags/v0.7.17.tar.gz -O $buildpath/bwa-0.7.17.tar.gz
wget -t 0 -c https://github.com/amplab/snap/archive/refs/tags/v2.0.1.tar.gz -O $buildpath/snap-2.0.1.tar.gz
wget -t 0 -c https://github.com/lpryszcz/last/archive/refs/heads/master.zip
wget -t 0 -c https://github.com/DLTcollab/sse2neon/raw/master/sse2neon.h

tar -zxvf redundans-0.13c.tar.gz && rm -rf redundans-0.13c.tar.gz
tar -zxvf pyScaf-0.12a.tar.gz && rm -rf pyScaf-0.12a.tar.gz
tar -zxvf FastaIndex-0.11c.tar.gz && rm -rf FastaIndex-0.11c.tar.gz
tar -zxvf bwa-0.7.17.tar.gz && rm -rf bwa-0.7.17.tar.gz
tar -zxvf snap-2.0.1.tar.gz && rm -rf snap-2.0.1.tar.gz
tar -zxvf GapCloser-src-v1.12-r6.tgz && rm -rf GapCloser-src-v1.12-r6.tgz
unzip master.zip && rm -rf master.zip

cp -rf pyScaf-0.12a/* redundans-0.13c/bin/pyScaf/ && rm -rf pyScaf-0.12a
cp -rf FastaIndex-0.11c/* redundans-0.13c/bin/pyScaf/FastaIndex/ && rm -rf FastaIndex-0.11c
cp -rf bwa-0.7.17/* redundans-0.13c/bin/bwa/ && rm -rf bwa-0.7.17
cp -rf snap-2.0.1/* redundans-0.13c/bin/snap/ && rm -rf snap-2.0.1
cp -rf last-master/* redundans-0.13c/bin/last/ && rm -rf last-master
cp sse2neon.h redundans-0.13c/bin/bwa/ 
cp sse2neon.h redundans-0.13c/bin/snap/SNAPLib/ && rm -rf sse2neon.h

cd $buildpath/redundans-0.13c/bin/parallel/
./configure && make clean && make

cd $buildpath/redundans-0.13c/bin/pyScaf/FastaIndex
python setup.py install

cd $buildpath/redundans-0.13c/bin/pyScaf/
python setup.py install

cd $buildpath/redundans-0.13c/bin/bwa
sed -i "s/gcc/clang/" Makefile
sed -i "29c #include \"sse2neon.h\"" ksw.c
sed -i "33c extern const uint8_t rle_auxtab[8];" rle.h
make

cd $buildpath/redundans-0.13c/bin/last
sed -i 's/(MAKE) CXXFLAGS/(MAKE) CXX=clang++ CXXFLAGS/g' makefile
make clean && make

cd $buildpath/redundans-0.13c/bin/snap
sed -i "2c \ \ CXXFLAGS = -O3 -Wno-format -std=c++98 -march=armv8.2-a" Makefile
sed -i "34c CXX = clang++" Makefile
sed -i "s/-msse//" Makefile
sed -i "81c #include \"sse2neon.h\"" SNAPLib/Compat.h
make


cd $buildpath/v1.12-r6/
sed -i "s/\\tg++/\\tclang++/" Release/makefile
sed -i "s/\\tg++/\\tclang++/" Release/subdir.mk
sed -i "s/hash(/hash_table(/" HashTable.hpp
make
\cp Release/GapCloser $buildpath/redundans-0.13c/bin/
rm -rf $buildpath/v1.12-r6
mkdir -p $installpath/redundans-0.13c && cp -r $buildpath/redundans-0.13c/* $installpath/redundans-0.13c

cat>"$installpath/redundans-0.13c/redundans_modulefiles"<<EOF
#%Module1.0
conflict redundans
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set REDUNDANS \$pwd
setenv REDUNDANS \$REDUNDANS
prepend-path PATH \$REDUNDANS
EOF

rm -rf $buildpath/redundans-0.13c

